import { DeepCopy } from "@/utils/Utils"
import { DictType } from "@/entities/Constant"

const lodash = require("lodash")
const Base = require('../entities/Base')
const { dictToWhere } = require('@/utils/sqlUtils')

const DictDao = {
    table: "dict",
    pageSize: Base.pageSize
}

DictDao.initialForm = () => {
    return JSON.parse(JSON.stringify(
        {
            id: 0,
            type: "",
            name: "",
            data: "",
            enable: true,
            parentId: 0,
            usageScopes: []
        }
    ))
}

DictDao.initialRules = () => {
    return JSON.parse(JSON.stringify(
        {
            name: [{
                required: true,
                message: "",
                trigger: "blur",
            }],
        }
    ))
}

DictDao.boolRow = (row) => {
    let obj = DeepCopy(row)
    obj.enable = Boolean(obj.enable)
    return obj
}

DictDao.toModule = (row) => {
    let obj = DictDao.boolRow(row)
    obj.id = 0
    obj.name = ""
    obj.parentId = row.id
    obj.type = DictType.JsModule
    return obj
}

DictDao.Platforms = async (dictType, usageScopes, parentId) => {
    if (!parentId) {
        parentId = 0
    }
    let whereDict = {
        type: dictType,
        parentId: parentId
    }

    let whereSql = dictToWhere(whereDict)
    console.log("whereSql", whereSql, usageScopes)
    let platforms = await DictDao.fetchAll(whereSql)
    if (lodash.isString(usageScopes)) {
        usageScopes = usageScopes.split(",")
    } else if (!lodash.isArray(usageScopes)) {
        if (usageScopes) {
            usageScopes = [usageScopes]
        } else {
            usageScopes = []
        }
    }
    return platforms.filter(platform => {
        if (lodash.isEmpty(usageScopes)) {
            return true
        }
        if (lodash.isEmpty(platform.usageScopes)) {
            return false
        }
        return platform.usageScopes.some(item => usageScopes.includes(item))
    })
}

DictDao.List = async (type) => {
    let whereDict = {}
    if (type) {
        whereDict = {
            enable: 1,
            type: type,
        }
    }
    let whereSql = dictToWhere(whereDict)
    return await DictDao.fetchAll(whereSql)
}

DictDao.fetchAll = async (whereSql) => {
    return await DictDao.fetchWhere(DictDao.table, whereSql)
}

DictDao.fetchWhere = async (table, where, limit = "", order = "order by id desc") => {
    try {
        const objs = await Base.fetchWhere(table, where, limit, order)
        return objs.filter(item => {
            item.usageScopes = JSON.parse(item.usageScopes)
            return true
        })
    } catch (error) {
        console.log(error)
        return []
    }
}

DictDao.save = async (obj) => {
    if (lodash.isArray(obj.usageScopes)) {
        obj.usageScopes = JSON.stringify(obj.usageScopes)
    }
    return await Base.save(DictDao.table, obj)
}

DictDao.delete = async (id) => {
    return await Base.delete(DictDao.table, id)
}

DictDao.fetchOne = async (id) => {
    let whereSql = "where id = " + id
    let res = await DictDao.fetchWhere(DictDao.table, whereSql, 'limit 1')
    if (res.length > 0) {
        return res[0]
    } else {
        return null
    }
}

export { DictDao }